package com.mengya.locks;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName SaleTicketDemo
 * @Description
 * @Author xiongwei.wu
 * @Date 2022/12/30 14:31
 **/
public class SaleTicketDemo {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();

        new Thread(() -> {for (int i = 0; i < 55; i++) {ticket.sale();}}, "a").start();
        new Thread(() -> {for (int i = 0; i < 55; i++) {ticket.sale();}}, "b").start();
        new Thread(() -> {for (int i = 0; i < 55; i++) {ticket.sale();}}, "c").start();
        new Thread(() -> {for (int i = 0; i < 55; i++) {ticket.sale();}}, "d").start();
        new Thread(() -> {for (int i = 0; i < 55; i++) {ticket.sale();}}, "e").start();
    }
}

class Ticket {
    private int number = 50;
    private Lock lock = new ReentrantLock(true);//默认为非公平锁

    public void sale() {
        lock.lock();
        try {
            if (number > 0) {
                System.out.println(Thread.currentThread().getName() + "\t 卖出第：" + (number--) + "\t 还剩下: " + number);
            }
        } finally {
            lock.unlock();
        }
    }
}